/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skymr.sms.dao;

import com.skymr.sms.model.Contactor;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author skymr
 */
public class ContactDao {
    public List<Contactor> queryAllContactors() throws Exception{
        Connection conn = ConnectionManager.getSqliteConnection();
        String sql = "select * from contactor";
        List<Contactor> list = new ArrayList<Contactor>();
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery(sql);
        while(rs.next()){
            Contactor c = new Contactor();
            c.setId(rs.getInt("id"));
            c.setName(rs.getString("name"));
            c.setCellphoneNum(rs.getString("cellphone"));
            list.add(c);
        }
        conn.close();
        return list;
    }
    
    public void addContactor(Contactor c) throws Exception{
        Connection conn = ConnectionManager.getSqliteConnection();
        String sql = "insert into contactor (name,cellphone) values ('"+c.getName()+"','" + c.getCellphoneNum()+"')";
        Statement s = conn.createStatement();
        s.executeUpdate(sql);
        conn.close();
    }
    
    public void updateContactor(Contactor c) throws Exception{
        Connection conn = ConnectionManager.getSqliteConnection();
        String sql = "update contactor set name="+c.getName() +",cellphone="+c.getCellphoneNum()+" where id="+c.getId();
        Statement s = conn.createStatement();
        s.executeUpdate(sql);
        conn.close();
    }
    
    public static void main(String[] args) throws Exception{
        Contactor c = new Contactor();
        c.setCellphoneNum("15859210350");
        c.setName("曹译天");
        new ContactDao().addContactor(c);
    }
}
